package com.ruoyix.weblog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.weblog.domain.bo.BlogSortBo;
import com.ruoyix.weblog.domain.vo.BlogSortVo;
import com.ruoyix.weblog.domain.BlogSort;
import com.ruoyix.weblog.mapper.BlogSortMapper;
import com.ruoyix.weblog.service.IBlogSortService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class BlogSortServiceImpl implements IBlogSortService {

    private final BlogSortMapper baseMapper;

    /**
     * 查询分类
     */
    @Override
    public BlogSortVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询分类列表
     */
    @Override
    public TableDataInfo<BlogSortVo> queryPageList(BlogSortBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogSort> lqw = buildQueryWrapper(bo);
        Page<BlogSortVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询分类列表
     */
    @Override
    public List<BlogSortVo> queryList(BlogSortBo bo) {
        LambdaQueryWrapper<BlogSort> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogSort> buildQueryWrapper(BlogSortBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogSort> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSortName()), BlogSort::getSortName, bo.getSortName());
        lqw.eq(StringUtils.isNotBlank(bo.getSortDescription()), BlogSort::getSortDescription, bo.getSortDescription());
        lqw.eq(bo.getSortType() != null, BlogSort::getSortType, bo.getSortType());
        lqw.eq(bo.getPriority() != null, BlogSort::getPriority, bo.getPriority());
        lqw.eq(StringUtils.isNotBlank(bo.getHomePageType()), BlogSort::getHomePageType, bo.getHomePageType());
        lqw.eq(StringUtils.isNotBlank(bo.getArticlePageType()), BlogSort::getArticlePageType, bo.getArticlePageType());
        return lqw;
    }

    /**
     * 新增分类
     */
    @Override
    public Boolean insertByBo(BlogSortBo bo) {
        BlogSort add = BeanUtil.toBean(bo, BlogSort.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改分类
     */
    @Override
    public Boolean updateByBo(BlogSortBo bo) {
        BlogSort update = BeanUtil.toBean(bo, BlogSort.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogSort entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public List<BlogSortVo> selectBlogListWithLabel() {
        return baseMapper.selectBlogListWithLabel();
    }
}
